<template>
  <div class="search-input">
    <i class="icon iconfont icon-sousuo"></i>
    <input
      class="input-inner"
      v-model="query"
      :placeholder="placeholder"
    />
    <i
      v-show="query"
      class="icon iconfont icon-delete"
      @click="clear"
    ></i>
  </div>
</template>

<script>
  import { debounce } from 'throttle-debounce'

  export default {
    name: 'search-input',
    props: {
      modelValue: String,
      placeholder: {
        type: String,
        default: '搜索歌曲、歌手'
      }
    },
    data() {
      return {
        query: this.modelValue
      }
    },
    created() {
      this.$watch('query', debounce(300, (newQuery) => {
        this.$emit('update:modelValue', newQuery.trim())
      }))

      this.$watch('modelValue', (newVal) => {
        this.query = newVal
      })
    },
    methods: {
      clear() {
        this.query = ''
      }
    }
  }
</script>

<style lang="scss" scoped>
.search-input {
  display: flex;
  align-items: center;
  box-sizing: border-box;
  width: 100%;
  padding: 0 6px;
  height: 32px;
  background: $theme-background-white;
  border-radius: 6px;
  .icon-sousuo {
    font-size: 20px;
    color: #222;
  }
  .input-inner {
    flex: 1;
    margin: 0 10px;
    line-height: 18px;
    background: $theme-background-white;
    color: #333;
    font-size: 14px;
    outline: 0;
    .icon-delete {
      font-size: 16px;
      color: #222;
    }
    &::placeholder {
      color: $theme-color-placeholder;
    }
  }
}
</style>
